package com.base.cn.platform.os.service.order.callback;

import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.entity.order.order.Order;
import com.base.cn.platform.os.entity.order.order.OrderCondition;
import com.base.cn.platform.os.entity.order.order.OrderDetails;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.order.order.OrderBiz;
import com.base.cn.platform.os.service.user.account.CusUserAccountBiz;
import com.base.cn.platform.os.service.user.member.CusUserMemberBuyRecordBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 修改流水状态处理类
 *
 * @author s.li
 * @create 2018-05-15-18:31
 */
@Service
public class OrderDetailsStatusHandle {

    @Autowired
    private CourseBuyRecordService courseBuyRecordService;
    @Autowired
    private CusUserMemberBuyRecordBiz cusUserMemberBuyRecordBiz;
    @Autowired
    private CusUserAccountBiz cusUserAccountBiz;
    @Autowired
    private OrderBiz orderBiz;

    /**
     * 关闭流水（用于课程、会员流水）
     * @param details
     */
    public void closeOrderDetails(OrderDetails details){
        //商品类型
        String shopType = details.getShopType();
        //关闭课程
        if(shopType.equals(ShopType.ORDER_SHOP_COURSE)){
            courseBuyRecordService.updateRecordCourseStatusByDetailsId(details.getId(), OrderStatus.CLOSE);
        }
        //关闭会员
        if(shopType.equals(ShopType.ORDER_SHOP_MEMBER)){
            //会员的有效天数
            int validDay = details.getValidDay();
            cusUserMemberBuyRecordBiz.updateMemberBuyRecordValidTime(details.getUserId(),validDay);
        }
    }

    /***
     * 流水退费
     * @param details 订单流水对象
     * @param sysUserId 管理员ID
     */
    public void refundOrderDetails(OrderDetails details,BigDecimal sysUserId) throws Exception{
        //流水金额
        double realPrice = details.getRealPrice()*details.getBuyNum().doubleValue();
        if(realPrice>0D){
            OrderCondition condition = new OrderCondition();
            condition.setId(details.getOrderId());
            Order order = orderBiz.findOrderInfo(condition);
            //银行现金支付
            double cashAmount = order.getCashAmount();
            //账户充值卡支付
            double vmAmount = order.getVmAmount();
            double orderRealPrice = order.getRealPrice()-order.getFreightPrice();
            //现金退款金额
            double _cashAmount = this.countRefundPrice(realPrice,cashAmount,orderRealPrice);
            //充值卡退费金额
            double _vmAmount = this.countRefundPrice(realPrice,vmAmount,orderRealPrice);
            if(_cashAmount>0){
                cusUserAccountBiz.updateAccountMoney(order.getUserId(), _cashAmount, "CASH", "REFUND", details.getOrderId(), details.getOrderNo(), null, true, "REFUND", sysUserId, 2,false);
            }
            if(_vmAmount>0){
                cusUserAccountBiz.updateAccountMoney(order.getUserId(), _vmAmount, "VM", "REFUND", details.getOrderId(), details.getOrderNo(), null, true, "REFUND", sysUserId, 2,false);
            }
        }

    }

    /**
     * 计算价格
     *
     * @param refundPrice 退款金额
     * @param orderAmount 订单所支付的现金金额都充值卡金额
     * @param orderRealPrice 订单实体支付总价（除去运费）
     * @return 得到要退款的金额
     */
    public double countRefundPrice(double refundPrice, double orderAmount,double orderRealPrice) {
        if (refundPrice > 0 && orderAmount > 0 && orderRealPrice > 0) {
            refundPrice = orderAmount / orderRealPrice * refundPrice;
            String strPrice = String.valueOf(refundPrice);
            boolean status = strPrice.contains(".");
            if(status){
                String[] arr = String.valueOf(strPrice).split("\\.");
                String pointNum = arr[1].length() < 2 ? arr[1] : arr[1].substring(0,2);
                String _price = arr[0]+"."+pointNum;
                return Double.parseDouble(_price);
            }else{
                return refundPrice;
            }
        }
        return 0D;
    }
}
